package com.bifrii.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.bifrii.transferObjects.Community;
import com.bifrii.transferObjects.Event;
import com.bifrii.transferObjects.Role;
import com.bifrii.transferObjects.User;


public class JDBCUserDAO implements IUserDAO {

	public List<Event> selectAllEvents(Integer idUser)
	{
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<Event> searchResults = new LinkedList<Event>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Event e, Assistance a where (a.idUser = ? ) and a.idEvent = e.idEvent";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idUser);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Event temp = new Event();
                temp.setOid(result.getString("oid"));
                temp.setIdEvent(result.getInt("idEvent"));
                temp.setIdCommunity(result.getInt("idCommunity"));
                temp.setIdCreator(result.getInt("idCreator"));
                temp.setDescription(result.getString("description"));
                temp.setLimitAssistance(result.getInt("limitAssistance"));
                searchResults.add(temp);
            }
            
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {
            }

        }
        return searchResults;
	}
	@Override
	public void insert(Integer idUser, User u) {
		
		Connection conn = ConnectionManager.getInstance().checkOut();
		
		String sql = "INSERT INTO user ( oid, idUser, user, password) VALUES (?, ?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, u.getOid());
            stmt.setInt(2, idUser);
            stmt.setString(3, u.getUser());
            stmt.setString(4, u.getPassword());
            

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }


	}

	@Override
	public void delete(Integer userId) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "DELETE FROM user WHERE (idUser = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }


	}

	@Override
	public List<User> selectAllUsers() {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<User> searchResults = new LinkedList<User>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM User order by idUser desc";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                User temp = new User();
                temp.setOid(result.getString("oid"));
                temp.setIdUser(result.getInt("idUser"));
                temp.setUser(result.getString("user"));
                temp.setPassword(result.getString("password"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

	@Override
	public User select(Integer userId) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        User u = null;
        String sql = "SELECT * FROM User WHERE (idUser = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            result = stmt.executeQuery();

            result.next();
            u = new User();
            u.setIdUser(result.getInt("idUser"));
            u.setUser(result.getString("user"));
            u.setPassword(result.getString("password"));            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return u;
	}
	
	public User select(String user) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        User u = null;
        String sql = "SELECT * FROM User WHERE (user = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, user);
            result = stmt.executeQuery();

            result.next();
            u = new User();
            u.setIdUser(result.getInt("idUser"));
            u.setUser(result.getString("user"));
            u.setPassword(result.getString("password"));            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return u;
	}
	@Override
	public List<Community> selectAllCommunities(Integer idUser) {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<Community> searchResults = new LinkedList<Community>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Community c, CommunityUserRole cur where (cur.idUser = ? ) and cur.idUser = c.idCommunity";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idUser);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	
            	Community temp = new Community();
            	temp.setOid(result.getString("oid"));
                temp.setIdCommunity(result.getInt("idCommunity"));
                temp.setIdCategory(result.getInt("idCategory"));
                temp.setIdCreator(result.getInt("idCreator"));
                temp.setDescription(result.getString("description"));
                temp.setAvatar(result.getString("avatar"));
                temp.setName(result.getString("name"));
                searchResults.add(temp);
            }
            
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {
            }

        }
        return searchResults;
	}
	@Override
	public Role selectRole(Integer userId) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        Role r = null;
        String sql = "SELECT * FROM CommunityUserRole cur, Role r WHERE (cur.idUser = ?) and cur.idUser = r.idRole";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            result = stmt.executeQuery();

            result.next();
            r = new Role();
            r.setOid(result.getString("oid"));
            r.setIdRole(result.getInt("idRole"));
            r.setName(result.getString("name"));
            r.setPrivilege(result.getString("privilege"));            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return r;
	}

}
